package com.example.generate;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import javax.sql.DataSource;

public class SystemGenerate {
    //数据库配置
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG=new DataSourceConfig
            .Builder(
                    "jdbc:mysql://127.0.0.1:3306/property_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
                    "root",
                    "20021218"
            );
    //全局配置
    private static GlobalConfig.Builder globalConfig(){
        return new GlobalConfig.Builder()
                //输出目录
                .outputDir("/src/main/java")
                //作者
                .author("kimido")
                //使用Swagger接口文档
                .enableSwagger()
                //使用基于jvm新的编程语言kotlin
                .enableKotlin()
                //时间使用的类型
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd");
    }

    //包配置
    private PackageConfig.Builder packageConfig(){
        return new PackageConfig.Builder()
                .parent("com.example.web.system")
                .entity("pojo")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .controller("controller");
    }
    //模板配置
    private TemplateConfig.Builder templateConfig(){
        return new TemplateConfig.Builder();
    }

    public static void main(String[] args) {
        String propertyPath = System.getProperty("user.dir");
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig((builder)->{
                    builder.author("kimido")
                            .enableSwagger()
                            .outputDir(propertyPath+"/property-web/src/main/java");
                })
                .packageConfig((builder)->{
                    builder.parent("com.example.web.system");
                })
                .strategyConfig((scanner,builder)->{
                    builder.addInclude(scanner.apply("输入表名，多个表用逗号隔开"))
                            .addTablePrefix("")
                            .entityBuilder()
                            .enableLombok()
                            .mapperBuilder()
                            .enableBaseColumnList()
                            .enableBaseResultMap()
                            .controllerBuilder()
                            .enableRestStyle();
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
